# -*- coding: UTF-8 -*-
'''
@Project: stock
@File: Hs300.py
@Notes: 
@Author: zhuyoucheng
@Date: 2021/3/23 0023 18:18
'''
from App.Middleware.BaseMiddleware import BaseMiddleware
from App.Model.PyMysql import PyMysql
from App.Model.Hs300StockCodeModel import Hs300StockCodeModel
import baostock as bs
import pandas as pd
import datetime

class Hs300(BaseMiddleware):

    def save_stock_code(self):
        lg = bs.login()
        # 显示登陆返回信息
        print('login respond error_code:' + lg.error_code)
        print('login respond  error_msg:' + lg.error_msg)
        # 获取上证50成分股
        rs = bs.query_hs300_stocks()
        print('query_sz50 error_code:' + rs.error_code)
        print('query_sz50  error_msg:' + rs.error_msg)
        hs300_stocks = []
        while (rs.error_code == '0') & rs.next():
            # 获取一条记录，将记录合并在一起
            try:
                code = rs.get_row_data()[1]
                name = rs.get_row_data()[2]
                val = {'stock_code': code, 'code_name': name}
                hs300_stocks.append(val)
            except Exception as e:
                print(e)

        Hs300StockCodeModel().create(hs300_stocks)
        pass
